/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpultility;

import java.io.Serializable;
import java.math.BigInteger;
import java.security.MessageDigest;

/**
 *
 * @author gilmario
 */
public class GilCripto implements Serializable {

    private static final long serialVersionUID = 1L;

    public static String md5(String senha) {
        String cripto = null;
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(senha.getBytes());
            BigInteger hash = new BigInteger(1, digest.digest());
            cripto = hash.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cripto;
    }

    public static String sha1(String senha) {
        String cripto = null;
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA1");
            digest.update(senha.getBytes());
            BigInteger hash = new BigInteger(1, digest.digest());
            cripto = hash.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cripto;
    }

    public static String sha2(String senha) {
        String cripto = null;
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            digest.update(senha.getBytes());
            BigInteger hash = new BigInteger(1, digest.digest());
            cripto = hash.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cripto;
    }

    public static String sha3(String senha) {
        String cripto = null;
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-512");
            digest.update(senha.getBytes());
            BigInteger hash = new BigInteger(1, digest.digest());
            cripto = hash.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cripto;
    }

    public static String hexaToString(String hex) {
        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < hex.length() - 1; i += 2) {
            //grab the hex in pairs
            String output = hex.substring(i, (i + 2));
            //convert hex to decimal
            int decimal = Integer.parseInt(output, 16);
            //convert the decimal to character
            sb.append((char) decimal);
            System.out.print((char) decimal);

            temp.append((char) decimal);
        }
        return temp.toString();
    }

    public static String convertStringToHex(String str) {
        char[] chars = str.toCharArray();
        StringBuilder hex = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            hex.append(Integer.toHexString((int) chars[i]));
        }
        return hex.toString();
    }

    public char[] Criptografar(String texto, int chave) {
        char[] cripto = new char[texto.length()];
        for (int i = 0; i < texto.length(); i++) {
            cripto[i] = texto.charAt(i);
            cripto[i] = (char) (cripto[i] + chave);

        }
        return cripto;
    }

    public String charToStr(char[] cripto) {
        String texto2 = "";
        if (cripto != null) {
            char[] c = cripto.clone();
            for (int i = 0; i < c.length; i++) {
                c[i] = (char) (c[i]);
                texto2 = texto2 + ((char) c[i]);
            }
        }
        return texto2;
    }

    public String DesCriptografar(char[] cripto, int chave) {
        String texto2 = "";
        if (cripto != null) {
            char[] c = cripto.clone();
            for (int i = 0; i < c.length; i++) {
                c[i] = (char) (c[i] - chave);
                texto2 = texto2 + ((char) c[i]);
            }
        }
        return texto2;
    }
}